<?php
declare(strict_types=1);

namespace App\Controller\WsWorker;

use App\Component\RedisGroupSever;
use App\Component\RedisUserSever;
use App\Component\Server;
use Hyperf\Utils\Codec\Json;

class Login
{
    public static function login($fd, $data)
    {
        $redisUser = new RedisUserSever();
        $token = $data["token"];
        $userId = $redisUser->getUserByToken($token);
        $user  = json::decode($redisUser->getUserInfo($userId));

        if (!$user) {
            $msg = [
                "type" => "loginAgain"
            ];
        } else {
            $msg = [
                "type" => "loginEd",
                "id"   => $user["id"],
                "name" => $user["name"],
                "img"  => $user["img"],
            ];
        }
        Server::sendToAll(Json::encode($msg), [$fd]);

    }
}